<?php
namespace App\Service\Member;

use Illuminate\Support\Facades\DB;

class ChatService extends BaseService
{
    //聊天室

    /***
     * 聊天室列表
     * @param array $pages
     */
    public function getList($pages)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = $this->dbConn($this->dbChat);
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])
            ->orderBy('id', 'desc')
            ->get();
        if (!count($res)) {
            return $this->toback();
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $thumb = file_exists($v['thumb']) ? env('DOMAIN').'/'.$v['thumb'] : '';
            $user_num = $this->dbConn($this->dbChatUid)
                ->where('del', 0)
                ->where('chat_id', $v['id'])
                ->count();
            $arys[] = [
                'id' => $v['id'],
                'title' => $v['title'],
                'thumb' => $thumb,
                'is_hot' => $v['is_hot'],
                'user_num' => $user_num,
            ];
        }
        return $this->toback(0, '', $arys, $total);
    }

    /**
     * 创建聊天室
     */
    public function add($pas, $uid)
    {
        $res = $this->dbConn($this->dbChat)
            ->where('del', 0)
            ->where('title', $pas['title'])
            ->first();
        if (!empty($res)) {
            return $this->toback(-2, '存在同名聊天室');
        }
        $pas['addtime'] = toDate();
        $lastid = $this->dbConn($this->dbChat)->insertGetId($pas);
        if (!$lastid) {
            return $this->toback(-1, 'error');
        }
        $this->dbConn($this->dbChatUid)->insertGetId([
            'chat_id' => $lastid, 'uid' => $uid, 'addtime' => toDate(),
        ]);
        $arys = [
            'chat_id' => $lastid,
        ];
        return $this->toback(0, '', $arys);
    }

    /***
     * 聊天室内容列表
     * @param int $id
     * @param int $uid
     * @param array $pages
     */
    public function cntList($id, $uid, $pages)
    {
        $arys = ['title' => '', 'user_num' => 0, 'cntList' => [], 'cntTotal' => 0];
        $chatOne = (array) $this->dbConn($this->dbChat)
            ->where('id', $id)
            ->first();
        if (empty($chatOne)) {
            return $this->toback(-2, '没有聊天室');
        }
        $arys['title'] = $chatOne['title'];
        $arys['user_num'] = $this->dbConn($this->dbChatUid)
            ->where('chat_id', $id)
            ->count();
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = $this->dbConn($this->dbChatCnt)->where('chat_id', $id);
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])
            ->orderBy('id', 'desc')
            ->get();
        if (!count($res)) {
            return $this->toback(0, '', $arys);
        }
        foreach ($res as $v) {
            $v = (array) $v;
            $type = $v['type'];
            $uOne = (array) $this->dbConn($this->dbUser)
                ->where('id', $v['uid'])
                ->first();
            $uname = '-';
            $uhead = '';
            if (!empty($uOne)) {
                $uname = $uOne['uname'];
                $uhead = file_exists($uOne['head']) ? env('DOMAIN').'/'.$uOne['head'] : '';
            }
            if ($type == 1) { //文字
                $cnt = $v['cnt'];
            } else if (in_array($type, [2, 3])) { //图片+表情
                if (substr($v['cnt'], 0, 4) == 'http') {
                    $cnt = $v['cnt'];
                } else {
                    $cnt = file_exists($v['cnt']) ? env('DOMAIN').'/'.$v['cnt'] : '';
                }
            } else { //病历
                $bingli = (array) $this->dbConn($this->dbUdsPatient)
                    ->where('id', $v['cnt'])
                    ->first();
                $cnt = empty($bingli) ? '' : $bingli['title'];
            }
            $arys['cntList'][] = [
                'id' => $v['id'],
                'uid' => $v['uid'],
                'utype' => $v['uid']==$uid ? 1 : 2, //1自己，2别人
                'type' => $v['type'],
                'cnt' => $cnt,
                'uname' => $uname,
                'uhead' => $uhead,
            ];
        }
        $arys['cntTotal'] = $total;
        return $this->toback(0, '', $arys);
    }

    /**
     * 发送内容
     */
    public function send($pas)
    {
        $pas['addtime'] = toDate();
        $lastid = $this->dbConn($this->dbChatCnt)->insertGetId($pas);
        if (!$lastid) {
            return $this->toback(-1, 'error');
        }
        return $this->toback();
    }

    /**
     * 表情包
     * @param array $pages【start，limit】
     */
    public function emojiList($pages)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = $this->dbConn($this->dbEmoji)
            ->where('del', 0);
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])
            ->orderBy('id', 'asc')
            ->get();
        if (!count($res)) {
            return $this->toback();
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            if (substr($v['link'], 0, 4) == 'http') {
                $link = $v['link'];
            } else {
                $link = file_exists($v['link']) ? env('DOMAIN').'/'.$v['link'] : '';
            }
            $arys[] = [
                'id' => $v['id'],
                'title' => $v['title'],
                'link' => $link,
            ];
        }
        return $this->toback(0, '', $arys, $total);
    }
}